<?php
declare (strict_types = 1);

namespace app\api\controller\v1;

use app\api\controller\Base;
use app\api\model\ProjectClass;
use app\api\model\ProjectClass as ProjectClassModel;
use app\api\model\ProjectClassStageRoute;
use app\api\model\ProjectInfo;
use app\api\model\ProjectInfoStageRoute;
use app\api\model\ProjectStage as ProjectStageModel;
use app\api\model\ProjectClassStageRoute as ProjectClassStageRouteModel;
use app\api\model\Project as ProjectModel;
use app\api\model\ProjectInfo as ProjectInfoModel;
use app\api\model\ProjectInfoStageRoute as ProjectInfoStageRouteModel;
use app\api\model\ProjectFiles as ProjectFilesModel;
use app\api\model\ProjectTeaminfo as ProjectTeaminfoModel;
use app\api\model\ProjectMeetingLog as ProjectMeetingLogModel;
use app\api\model\User as UserModel;
use app\Request;
use think\facade\Db;
use think\facade\Config;

class Project extends Base
{

    /**
     * 获取项目列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function projecMytlist(Request $request){
        $keyword = $request->param('keyword');
        $uid = $request->param('uid');
        $where = [];
        if($uid){
            $where['pin.project_pm'] = $uid;
        }
        $ProjectInfoModel = new ProjectInfoModel;
        $list = $ProjectInfoModel->alias('pin')
            ->field('pin.id,pin.project_id,pin.project_no,pin.project_class,pin.project_name,pin.project_des,
                                   c.name as class_name')
            ->leftjoin('project_class c','pin.project_class = c.id')
            ->where("pin.project_name","like","%$keyword%")
            ->where($where)
            ->select();

        if($list){
            $result = ["status"=>true,"msg"=>"获取列表成功！","data"=>$list];
        }else{
            $result = ["status"=>false,"msg"=>"获取列表失败！"];
        }
        return json($result);
    }

    /**
     * 根据项目类别id获取项目阶段信息
     * @param Request $request
     * @return \think\response\Json
     */
    public function getStageCurrInfo(Request $request){
        $CurrTime = time();
        $project_id = $request->param('project_id');
        $class_id = $request->param('class_id');
        $ProjectInfoStageRouteModel= new ProjectInfoStageRouteModel;

//        $data = $ProjectInfoStageRouteModel->where('project_id',$project_id)->where('class_id','=',$class_id)->select();
//        foreach ($data as $k=>$v){
//            $data[$k]['start_time'] = date("Y-m-d H:i:s",$v['start_time']);
//            $data[$k]['end_time'] = date("Y-m-d H:i:s",$v['end_time']);
//        }
//        return json($data);

        $data = $ProjectInfoStageRouteModel->alias('i')
            ->field('s.id,s.name,i.start_time,i.end_time,i.status')
            ->where('class_id','=',$class_id)
            ->where('start_time','<',$CurrTime)
            ->where('end_time','>',$CurrTime)
            ->where('stage_id','>',0)
            ->where('project_id',$project_id)
            ->leftjoin('project_stage s','i.stage_id = s.id')
            ->find();

        //return $ProjectInfoStageRouteModel->getLastSql();

        $count = $ProjectInfoStageRouteModel->where('class_id','=',$class_id)
            ->where('project_id',$project_id)
            ->where('start_time','<',$CurrTime)
            ->where('end_time','>',$CurrTime)
            ->where('stage_id','>',0)
            ->count();
        if($data){
            $data['start_time'] = date("Y-m-d H:i:s",$data['start_time']);
            $data['end_time'] = date("Y-m-d H:i:s",$data['end_time']);
        }
        if($count){
            $flage = "阶段时间设计不合理，有阶段时间交叉";
        }else{
            $flage = "";
        }
        if($data){
            $result = ["status"=>true,"msg"=>"获取成功！","data"=>$data,'currtime'=>date("Y-m-d H:i:s",$CurrTime),'flage'=>$flage];
        }else{
            $result = ["status"=>false,"msg"=>"获取失败！"];
        }
        return json($result);
    }
    /**
     * 禅道项目列表
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function projectlist(Request $request){
        try {

            $page = $request->param('page');
            $pagesize = $request->param('pagesize');
            $project = new ProjectModel();
            $list = $project->where('deleted','=','0')->where($where)->order('id','desc')->paginate([
                'list_rows'=> $pagesize,
                'var_page' => 'page',
                'page'=>$page
            ]);
            if($list){
                $result = ["status"=>true,"msg"=>"获取列表成功！","data"=>$list];
            }else{
                $result = ["status"=>false,"msg"=>"获取列表失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 项目列表（带分页）
     * @param Request $request
     * @return \think\response\Json
     */
    public function projectlistpage(Request $request){
        try {
            $CurrTime = time();
            $CurrTimeMore = $CurrTime-86400;
            $CurrMonth = strtotime(date('Y-m'));
            $uid = $request->param('uid');
            $stage_id = $request->param('stage_id');
            $start_s_time = $request->param('start_s_time');
            $start_e_time = $request->param('start_e_time');
            $end_s_time = $request->param('end_s_time');
            $end_e_time = $request->param('end_e_time');
            $project_status = $request->param('project_status');
            $is_check = $request->param('is_check');

            $where = [];
            if($uid) $where[] = [["p.project_pm","=",$uid]];
            if($request->param('project_name')) $where[] =  [["p.project_name",'LIKE', "%".$request->param('project_name')."%"]];
            if($request->param('project_pmname')) $where[] = [['u.realname','LIKE',"%".$request->param('project_pmname')."%"]];
            if($request->param('project_level')) $where[] = [['p.project_level','LIKE',$request->param('project_level')]];
            if($stage_id) $where[] = [['r.stage_id','=',$stage_id]];
            if($start_s_time && $start_e_time) $where[] = [['p.p_s_time','<=',strtotime($start_s_time)],['p.p_e_time','>=',strtotime($start_e_time)]];
            if($end_s_time && $end_e_time) $where[] = [['p.p_s_time','<=',strtotime($end_s_time)],['p.p_e_time','>=',strtotime($end_e_time)]];
            if($project_status) $where[] = [['p.status','=',$project_status]];
            if($is_check) {
                if($is_check == 2){
                    $where[] = ['p.is_check','=',0];
                }else{
                    $where[] = ['p.is_check','=',$is_check];
                }
            }

            $page = $request->param('page');
            $pagesize = $request->param('pagesize');
            $ProjectInfo = new ProjectInfoModel;
            $list = $ProjectInfo->alias('p')
                ->field('p.id,p.project_no,p.project_class as class_id,p.project_name,p.iskey,p.status as project_status,p.reason as project_reason,c.name as project_classname,p.project_level,
                                u.realname as project_pmname,s.id as stage_id,s.name as stage_name,m.main_content as month_plan,r.status as curr_status,r.reason,p.is_check
                                ')
                ->leftjoin('user u','p.project_pm = u.id')
                ->leftjoin('project_class c','p.project_class = c.id')
                ->leftjoin('project_info_stage_route r','r.project_id = p.id and r.class_id = p.project_class and '.$CurrTime.'>=r.start_time   and r.end_time >'.$CurrTimeMore)
                ->leftjoin('project_months m','p.id = m.project_id  and month ='.$CurrMonth)
                ->leftjoin('project_stage s','r.stage_id = s.id')
                ->where($where)
                ->order('p.project_no','desc')->paginate([
                    'list_rows'=> $pagesize,
                    'var_page' => 'page',
                    'page'=>$page
                ]);

            $sql = $ProjectInfo->getLastSql();
            $tr = "";

            foreach ($list as $v){
                if($v['curr_status'] == "-2"){
                    $v['curr_status'] = "未开始";
                }else if( $v['curr_status'] == "-1"){
                    $v['curr_status'] = "异常";
                }else if($v['curr_status'] == 0){

                    if($v['project_status'] == "-2"){
                        $v['curr_status'] = "未开始";
                    }else if($v['project_status'] == "-1"){
                        $v['curr_status'] = "异常";
                    }else if($v['project_status'] == "1"){
                        $v['curr_status'] = "正常";
                    }else if($v['project_status'] == "2"){
                        $v['curr_status'] = "暂停";
                    }else if($v['project_status'] == "3"){
                        $v['curr_status'] = "试运行";
                    }else if($v['project_status'] == "4"){
                        $v['curr_status'] = "运维";
                    }else if($v['project_status'] == "5"){
                        $v['curr_status'] = "结束";
                    }else{
                        $v['curr_status'] = "无匹配阶段";
                    }
                    $v['status'] = $v['project_status'];
                    $v['status_reson'] = $v['project_reason'];
                    $v['reason'] = $v['project_reason'];

                }else if( $v['curr_status'] == "1"){
                    $v['curr_status'] = "正常";
                }else if( $v['curr_status'] == "2"){
                    $v['curr_status'] = "暂停";
                }else if( $v['curr_status'] == "3"){
                    $v['curr_status'] = "试运行";
                }else if( $v['curr_status'] == "4"){
                    $v['curr_status'] = "运维";
                }else if( $v['curr_status'] == "5"){
                    $v['curr_status'] = "结束";
                }
                $v['status_reson'] = $v['reason'];
                $v['status'] = $v['curr_status'];
            }

            if($list){
                $result = ["status"=>true,"msg"=>"获取列表成功！","data"=>$list];
            }else{
                $result = ["status"=>false,"msg"=>"获取列表失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 根据项目经理UID获取参与考核的项目
     * @param Request $request
     * @return \think\response\Json
     */
    public function projectlistpagewithchage(Request $request){
        try {
            $CurrTime = time();
            $CurrTimeMore = $CurrTime-86400;
            $CurrMonth = strtotime(date('Y-m'));
            $uid = $request->param('uid');
            $where = [];
            $where[] = ['is_check','=',1];
            $where[] = [["p.project_pm","=",$uid]];
            $page = $request->param('page');
            $pagesize = $request->param('pagesize');
            $ProjectInfo = new ProjectInfoModel;
            $list = $ProjectInfo->alias('p')
                ->field('p.id,p.project_no,p.project_class as class_id,p.project_name,p.iskey,p.status as project_status,p.reason as project_reason,c.name as project_classname,p.project_level,
                                u.realname as project_pmname,s.id as stage_id,s.name as stage_name,m.main_content as month_plan,r.status as curr_status,r.reason,p.is_check
                                ,l.max_score,l.min_score')
                ->leftjoin('user u','p.project_pm = u.id')
                ->leftjoin('project_class c','p.project_class = c.id')
                ->leftjoin('project_info_stage_route r','r.project_id = p.id and r.class_id = p.project_class and '.$CurrTime.'>=r.start_time   and r.end_time >'.$CurrTimeMore)
                ->leftjoin('project_months m','p.id = m.project_id  and month ='.$CurrMonth)
                ->leftjoin('project_level l','p.project_level = l.name')
                ->leftjoin('project_stage s','r.stage_id = s.id')
                ->where($where)
                ->order('p.project_no','desc')->paginate([
                    'list_rows'=> $pagesize,
                    'var_page' => 'page',
                    'page'=>$page
                ]);

            $data_tmp = $list->all();
            foreach($data_tmp as $k=>$v){
                $v['project_name'] = $v['project_name'].'（'.$v['min_score'].'~~'.$v['max_score'].'）';
                $list[$k] = $v;
            }

            $sql = $ProjectInfo->getLastSql();
            $tr = "";

            foreach ($list as $v){
                if($v['curr_status'] == "-2"){
                    $v['curr_status'] = "未开始";
                }else if( $v['curr_status'] == "-1"){
                    $v['curr_status'] = "异常";
                }else if($v['curr_status'] == 0){

                    if($v['project_status'] == "-2"){
                        $v['curr_status'] = "未开始";
                    }else if($v['project_status'] == "-1"){
                        $v['curr_status'] = "异常";
                    }else if($v['project_status'] == "1"){
                        $v['curr_status'] = "正常";
                    }else if($v['project_status'] == "2"){
                        $v['curr_status'] = "暂停";
                    }else if($v['project_status'] == "3"){
                        $v['curr_status'] = "试运行";
                    }else if($v['project_status'] == "4"){
                        $v['curr_status'] = "运维";
                    }else if($v['project_status'] == "5"){
                        $v['curr_status'] = "结束";
                    }else{
                        $v['curr_status'] = "无匹配阶段";
                    }
                    $v['status'] = $v['project_status'];
                    $v['status_reson'] = $v['project_reason'];
                    $v['reason'] = $v['project_reason'];

                }else if( $v['curr_status'] == "1"){
                    $v['curr_status'] = "正常";
                }else if( $v['curr_status'] == "2"){
                    $v['curr_status'] = "暂停";
                }else if( $v['curr_status'] == "3"){
                    $v['curr_status'] = "试运行";
                }else if( $v['curr_status'] == "4"){
                    $v['curr_status'] = "运维";
                }else if( $v['curr_status'] == "5"){
                    $v['curr_status'] = "结束";
                }
                $v['status_reson'] = $v['reason'];
                $v['status'] = $v['curr_status'];
            }

            if($list){
                $result = ["status"=>true,"msg"=>"获取列表成功！","data"=>$list];
            }else{
                $result = ["status"=>false,"msg"=>"获取列表失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }
    /**
     * 项目立项
     * @param Request $request
     * @return \think\response\Json
     */
    public function add(Request $request){
        try {

            Db::startTrans();
            $data['project_no'] = ProjectNo($request->param('project_no'));
            //$data['project_no'] = $request->param('project_no');
            $data['uid'] = $request->param('uid');
            if($request->param('project_name')) $data['project_name'] = $request->param('project_name');
            if($request->param('project_des')) $data['project_des'] = $request->param('project_des');
            if($request->param('project_class')) $data['project_class'] = $request->param('project_class');
            if($request->param('project_pm')) $data['project_pm'] = $request->param('project_pm');
            if($request->param('project_skiller')) $data['project_skiller'] = $request->param('project_skiller');
            if($request->param('project_stageroute_id')) $data['project_stageroute_id'] = $request->param('project_stageroute_id');
            if($request->param('project_level')) $data['project_level'] = $request->param('project_level');
            $data['create_time'] = time();
            $class_id  =  $request->param('project_class');

            //项目编号为主键
            $ProjectInfo = new ProjectInfoModel;
            $project_no = $data['project_no'];
            $t = $ProjectInfo->where('project_no','like',$project_no)->find();
            if($t){
                Db::rollback();
                $result_t = ["status"=>false,"re_id"=>"","msg"=>"项目编号不能重复！"];
                return json($result_t);
            }

            $ProjectInfo = new ProjectInfoModel;
            $re = $ProjectInfo->insertGetId($data);

            //保存时间
            $t = $request->param('project_stageroute_object');
            $project_stagerout_object =  $t;

            $routedata = [];
            if(isset($project_stagerout_object)){
                foreach ($project_stagerout_object as $key=>$v){
                    $tmp['stage_id'] = $v['stage_id'];
                    $tmp['project_id'] = $re;
                    $tmp['class_id'] = $class_id;
                    $tmp['start_time'] = strtotime($v['start_time']);
                    $tmp['end_time'] = strtotime($v['end_time']);
                    $routedata[] = $tmp;
                }
                $ProjectInfoStageRoute = new ProjectInfoStageRouteModel;
                $reroute = $ProjectInfoStageRoute->insertAll($routedata);
            }

            //更新项目主表开始和结算时间
            $project_count = count($routedata);
            $s_time = $routedata[0]['start_time'];
            $e_time = $routedata[$project_count-1]['end_time'];
            $ProjectInfoModel = new ProjectInfoModel;
            $d['p_s_time'] = $s_time;
            $d['p_e_time'] = $e_time;
            $update = $ProjectInfoModel->where('id','=',$re)->save($d);

            //保存附件地址
            $tt = $request->param('project_file');
            //$stage_id = $request->param('stage_id');
            $ProjectFile = $tt;
            $filedata = [];
            if(isset($ProjectFile)){
                foreach ($ProjectFile as $vv){
                    $vv['project_id'] = $re;
                    $vv['create_time'] = time();
                    $filedata[] =  $vv;
                }
                $fileMode = new ProjectFilesModel;
                $fileresult = $fileMode->insertAll($filedata);
            }else{
                $fileresult = true;
            }

            //保存团队成员
            $ttt = $request->param('teamlist');
            $teamlist = $ttt;
            $teamdata = [];
            if(isset($teamlist)){
                foreach($teamlist as $vvv){
                    $vvv['project_id'] =  $re;
                    $vvv['create_time'] =  time();
                    $teamdata[] = $vvv;
                }
                $teamMode = new ProjectTeaminfoModel;
                $teamresult = $teamMode->insertAll($teamdata);
            }

            if($re && $reroute ){
                Db::commit();
                $result = ["status"=>true,"re_id"=>$re,"msg"=>"添加成功！"];
            }else{
                Db::rollback();
                $result = ["status"=>false,"re_id"=>"","msg"=>"添加失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            Db::rollback();
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 检查项目编号的重复问题
     * @param Request $request
     * @return \think\response\Json
     */
    public function findProjectNo(Request $request){
        try {

            $pex = Config::get('app.project_name');
            $project_no = $request->param('project_no');
            $arr = explode($pex,$project_no);
            if($arr[0]){
                $project_no = ProjectNo($arr[0]);
            }
            $ProjectInfo = new ProjectInfoModel();
            $res = $ProjectInfo->where("project_no","LIKE",$project_no)->find();
            if($res){
                $result = ["status"=>true,"msg"=>"项目编号重复了！"];
            }else{
                $result = ["status"=>false,"msg"=>"项目编号未重复！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 项目编辑
     * @param Request $request
     * @return \think\response\Json
     */
    public function edit(Request $request){
        try {

            Db::startTrans();
            $project_id = $request->param('project_id');
            //$data['project_no'] = ProjectNo("1");
            $data['project_no'] = $request->param('project_no');
            if($request->param('project_name')) $data['project_name'] = $request->param('project_name');
            if($request->param('project_des')) $data['project_des'] = $request->param('project_des');
            if($request->param('project_class')) $data['project_class'] = $request->param('project_class');
            if($request->param('project_pm')) $data['project_pm'] = $request->param('project_pm');
            if($request->param('project_skiller')) $data['project_skiller'] = $request->param('project_skiller');
            if($request->param('project_stageroute_id')) $data['project_stageroute_id'] = $request->param('project_stageroute_id');
            if($request->param('project_level')) $data['project_level'] = $request->param('project_level');
            $data['create_time'] = time();
            $class_id  =  $request->param('project_class');

            $ProjectInfo = new ProjectInfoModel;
            $re = $ProjectInfo->where('id','=',$project_id)->save($data);

            //保存时间
            $ProjectInfoStageRoute = new ProjectInfoStageRouteModel;
            //先把数据取出
            $tmp_data_ProjectInfoStageRoute = $ProjectInfoStageRoute->where('project_id','=',$project_id)->select();
            if(isset($tmp_data_ProjectInfoStageRoute)){
                $tmp_data_ProjectInfoStageRoute = $tmp_data_ProjectInfoStageRoute->toArray();
            }
            //print_r($tmp_data_ProjectInfoStageRoute);
            $ProjectInfoStageRoute->where('project_id','=',$project_id)->delete();

            $t = $request->param('project_stageroute_object');
            $project_stagerout_object =  $t;

            //保存阶段时间
            $routedata = [];
            $tmp = [];
            if(isset($project_stagerout_object)){
                foreach ($project_stagerout_object as $key=>$v){

                    if(isset($tmp_data_ProjectInfoStageRoute)){
                        foreach ($tmp_data_ProjectInfoStageRoute as $ivo) {;
                            if($ivo['stage_id']==$v['stage_id']){
                                $tmp['stage_id'] = $ivo['stage_id'];
                                $tmp['project_id'] = $project_id;
                                $tmp['class_id'] = $class_id;
                                if(isset($ivo['reason'])) {
                                    $tmp['reason'] = $ivo['reason'];
                                }else{
                                    $tmp['reason'] = null;
                                }
                                $tmp['start_time'] = strtotime($v['start_time']);
                                $tmp['end_time'] = strtotime($v['end_time']);
                                $tmp['status'] = $ivo['status'];
                            }
                        }
                    }else{
                        $tmp['stage_id'] = $v['stage_id'];
                        $tmp['project_id'] = $project_id;
                        $tmp['class_id'] = $class_id;
                        $tmp['start_time'] = strtotime($v['start_time']);
                        $tmp['end_time'] = strtotime($v['end_time']);
                    }
                    $routedata[] = $tmp;

                }
                //print_r($routedata);
                $ProjectInfoStageRoute = new ProjectInfoStageRouteModel;
                $reroute = $ProjectInfoStageRoute->insertAll($routedata);
            }

            //更新项目主表开始和结算时间
            $project_count = count($routedata);
            $s_time = $routedata[0]['start_time'];
            $e_time = $routedata[$project_count-1]['end_time'];
            $ProjectInfoModel = new ProjectInfoModel;
            $d['p_s_time'] = $s_time;
            $d['p_e_time'] = $e_time;
            $update = $ProjectInfoModel->where('id','=',$project_id)->save($d);

            //保存附件地址
            $fileMode = new ProjectFilesModel;
            $fileMode->where('project_id','=',$project_id)->delete();

            $tt = $request->param('project_file');
            //$stage_id = $request->param('stage_id');
            $ProjectFile = $tt;
            $filedata = [];
            if(isset($ProjectFile)){
                foreach ($ProjectFile as $vv){
                    $vv['project_id'] = $project_id;
                    $vv['create_time'] = time();
                    $filedata[] =  $vv;
                }
                $fileMode = new ProjectFilesModel;
                $fileresult = $fileMode->insertAll($filedata);
            }else{
                $fileresult = true;
            }

            //保存团队成员
            $teamMode = new ProjectTeaminfoModel;
            //$teamMode->where('project_id','=',$project_id)->delete();
            $team_data['status'] = "-1";
            $teamMode->where('project_id','=',$project_id)->save($team_data);

            $ttt = $request->param('teamlist');
            $teamlist = $ttt;
            $teamdata = [];
            if(isset($teamlist)){
                foreach($teamlist as $vvv){
                    $vvv['project_id'] =  $project_id;
                    $vvv['create_time'] =  time();
                    $teamdata[] = $vvv;
                }
                $teamMode = new ProjectTeaminfoModel;
                $teamresult = $teamMode->insertAll($teamdata);
            }

            if(($re!==false) && $reroute){
                Db::commit();
                $result = ["status"=>true,"msg"=>"保存成功！"];
            }else{
                Db::rollback();
                $result = ["status"=>false,"msg"=>"保存失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 项目删除
     * @param Request $request
     * @return \think\response\Json
     */
    public function del(Request $request){
        try {
            $project_id = $request->param('project_id');
            $ProjectInfo = new ProjectInfoModel();
            $del = $ProjectInfo->where('id','=',$project_id)->delete();
            if($del){
                $result = ['msg'=>"删除成功！",'status'=> true];
            }else{
                $result = ['msg'=>"删除失败！",'status'=> false];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }
    /**
     * 设置项目状态（是否重点项目）
     * @param Request $request
     * @return \think\response\Json
     */
    public function projectsetstatus(Request $request){
        try {

            $project_id = $request->param('project_id');
            $status = $request->param('status');
            $model = new ProjectInfo();
            $data['iskey'] = $status;
            $res = $model->where('id','=',$project_id)->save($data);
            if($res!==false){
                $resutl = ['status'=>true,'msg'=>'设置重点项目成功！'];
            }else{
                $resutl = ['status'=>false,'msg'=>"设置重点项目失败！"];
            }
            return json($resutl);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }
    /**
     * 设置项目阶段状态
     * @param Request $request
     * @return \think\response\Json
     */
    public function setprojectstagestatus(Request $request){
        try {

            $stage_id = $request->param('stage_id');
            $class_id = $request->param('class_id');
            $project_id = $request->param('project_id');
            $status = $request->param('status');
            $reason = $request->param('reason');
            $model = new ProjectInfoStageRoute();
            $data['reason'] = $reason;
            $data['status'] = $status;

            $info = $model->where('project_id','=',$project_id)
                ->where('class_id','=',$class_id)
                ->where('stage_id','=',$stage_id)
                ->find();

            if($info){

                $res = $model->where('project_id','=',$project_id)
                    ->where('class_id','=',$class_id)
                    ->where('stage_id','=',$stage_id)
                    ->save($data);

                $projectInfo = new ProjectInfoModel();
                $re = $projectInfo->where('id','=',$project_id)->save($data);

                if($res !== false && $re!==false){
                    $result = ['status'=>true,'msg'=>'设置状态成功'];
                }else{
                    $result = ['status'=>false,'msg'=>'设置状态失败'];
                }
                return json($result);

            }else{

                $projectInfo = new ProjectInfoModel();
                $re = $projectInfo->where('id','=',$project_id)->save($data);

                if($re !== false){
                    $result = ['status'=>true,'msg'=>'设置状态成功!'];
                }else{
                    $result = ['status'=>false,'msg'=>'设置状态失败!'];
                }
                return json($result);

            }

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }
    /**
     * 项目详情
     * @param Request $request
     * @return \think\response\Json
     */
    public function detail(Request $request){
        try {
            $curr_time = time();
            $CurrMonth = strtotime(date('Y-m'));
            $project_id = $request->param('project_id');
            $ProjectInfoModel = new ProjectInfoModel();
            $detail = $ProjectInfoModel->alias('p')
                ->field('
                                          p.id as project_id,p.project_no,p.project_name,p.project_class,c.name as class_name,p.project_level,
                                          p.project_des,p.project_pm,p.project_skiller,u.realname as project_pm_name,m.main_content as month_plan
                                       ')
                ->leftjoin('project_class c','p.project_class = c.id')
                ->leftjoin('user u','p.project_pm = u.id')
                ->leftjoin('project_months m','p.id = m.project_id  and month ='.$CurrMonth)
                ->where('p.id','=',$project_id)
                ->find();



            if(!$detail['month_plan']){
                $detail['month_plan'] = "";
            }

            $User = new UserModel();
            $UserInfo = $User->where('id','=',$detail['project_skiller'])->find();
            if($UserInfo){
                $detail['project_skiller_name'] = $UserInfo['realname'];
            }else{
                $detail['project_skiller_name'] = "";
            }


            $projectTeamInfo = new ProjectTeaminfoModel();
            $teamInfo = $projectTeamInfo->field('uid,zn_name')
                ->where("project_id","=",$project_id)
                ->where('status','=',1)
                ->select();

            $detail['teamlist'] = $teamInfo;
            if($teamInfo){
                $detail['teamlist'] = $teamInfo;
            }else{
                $detail['teamlist'] = [];
            }

            $ProjectFiles = new ProjectFilesModel();
            $fielsInfo = $ProjectFiles->field('name,path')->where("project_id","=",$project_id)->select();
            $detail['project_file'] = $fielsInfo;
            if($fielsInfo){
                $detail['project_file'] = $fielsInfo;
            }else{
                $detail['project_file'] = [];
            }

            $ProjectInfoStageRoute = new ProjectInfoStageRouteModel();
            $infoStage = $ProjectInfoStageRoute->alias('i')
                ->field('s.name as stage_name,stage_id,start_time,end_time')
                ->leftjoin('project_stage s','i.stage_id =s.id')
                ->where("project_id","=",$project_id)
                ->order('i.sort desc,i.id asc')
                ->select();
            if($infoStage){
                foreach ($infoStage as $v){
                    if(($curr_time>=$v['start_time']) && ($curr_time<=$v['end_time'])){
                        $v['curr_stage'] = true;
                        $v['rgb_color'] = "rgb(67, 160, 71)";
                    }else if(($curr_time> $v['start_time']) && ($curr_time> $v['end_time'])){
                        $v['curr_stage'] = true;
                        $v['rgb_color'] = "rgb(25, 118, 210)";
                    }else{
                        $v['curr_stage'] = false;
                        $v['rgb_color'] = "rgb(144, 164, 174)";
                    }
                }

                foreach ($infoStage as $k=>$v){
                    $v['start_time'] = date("Y-m-d",$v['start_time']);
                    $v['end_time'] = date("Y-m-d",$v['end_time']);
                }
                $detail['project_stageroute_object'] = $infoStage;
            }

            if($detail){
                $result = ["status"=>true,"msg"=>"获取详情成功！","data"=>$detail];
            }else{
                $result = ["status"=>false,"msg"=>"获取详情失败！"];
            }
            return json($result);
        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 会议事项列表
     * @param Request $request
     * @return \think\response\Json]
     */
    public function meetinglist(Request $request){
        try {

            $project_id = $request->param('project_id');
            $page = $request->param('page');
            $pagesize = $request->param('pagesize');
            $ProjectMeetingLog = new ProjectMeetingLogModel();
            $list = $ProjectMeetingLog->order('id','desc')
                ->where('project_id','=',$project_id)
                ->where('status','>=','0')
                ->paginate([
                    'list_rows'=> $pagesize,
                    'var_page' => 'page',
                    'page'=>$page
                ])->each(function($item, $key){
                    $item['date_time'] = date("Y-m-d H:i:s",$item['date_time']);
                    return $item;
                });

            if($list){
                $result = ["status"=>true,"msg"=>"获取列表成功！","data"=>$list];
            }else{
                $result = ["status"=>false,"msg"=>"获取列表失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**\
     * 会议事项添加
     * @param Request $request
     * @return \think\response\Json
     */
    public function meetingadd(Request $request){
        try {

            $uid = $request->param('uid');
            $project_id = $request->param('project_id');
            $date_time = $request->param('date_time');
            $content_list = $request->param('content_list');

            $content = [];
            foreach($content_list as $k=>$v){
                $v['uid'] = $uid;
                $v['project_id'] = $project_id;
                $v['date_time'] = strtotime($date_time);
                $v['create_time'] = time();
                $content[] = $v;
            }

            $ProjectMeetingLogModel = new ProjectMeetingLogModel;
            $re = $ProjectMeetingLogModel->insertAll($content);
            if($re){
                $result = ["status"=>true,"msg"=>"添加成功！"];
            }else{
                $result = ["status"=>false,"msg"=>"添加失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 会议事项编辑
     * @param Request $request
     * @return \think\response\Json
     */
    public function meetingedit(Request $request){
        try {
            $id = $request->param('id');
            $content = $request->param('content');
            $ProjectMeetingLogModel = new ProjectMeetingLogModel;
            $data['content'] = $content;
            $data['update_time'] = time();

            $re = $ProjectMeetingLogModel->where("id",'=',$id)->save($data);
            if($re!==false){
                $result = ["status"=>true,"msg"=>"修改成功！"];
            }else{
                $result = ["status"=>false,"msg"=>"修改失败！"];
            }
            return json($result);
        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 设置会议状态
     * @param Request $request
     * @return \think\response\Json
     */
    public function setmeetingstatus(Request $request){
        try {
            $id = $request->param('id');
            $status = $request->param('status');
            $reason = $request->param('reason');
            $ProjectMeetingLogModel = new ProjectMeetingLogModel;
            $data['status'] = $status;
            $data['reason'] = $reason;
            $data['update_time'] = time();

            $re = $ProjectMeetingLogModel->where("id",'=',$id)->save($data);
            if($re!==false){
                $result = ["status"=>true,"msg"=>"设置成功！"];
            }else{
                $result = ["status"=>false,"msg"=>"设置失败！"];
            }
            return json($result);
        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }
    /**
     * 会议事项删除
     * @param Request $request
     * @return \think\response\Json
     */
    public function meetingdel(Request $request){
        try {
            $id = $request->param('id');
            $ProjectMeetingLogModel = new ProjectMeetingLogModel;
            $data['status'] = "-1";
            $data['update_time'] = time();

            $re = $ProjectMeetingLogModel->where("id",'=',$id)->save($data);
            if($re!==false){
                $result = ["status"=>true,"msg"=>"删除成功！"];
            }else{
                $result = ["status"=>false,"msg"=>"删除失败！"];
            }
            return json($result);
        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }
    /**
     * 项目类别列表
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function classlist(Request $request)
    {
        $classid = $request->param('id');
        $where = [];
        if($classid) $where[]  =  ['id','NOT IN',''.$classid.''] ;
        $ProjectClass = new ProjectClassModel();
        $list = $ProjectClass->where($where)->select();
        if($list) $resutl = ["status"=>true,"data"=>["list"=>$list],"msg"=>"获取列表成功！"];
        else $resutl = ["status"=>false,"data"=>"","msg"=>"获取列表失败！"];
        return json($resutl);
    }
    /**
     * 添加/编辑类别
     * @param Request $request
     * @return \think\response\Json
     */
    public function saveclass(Request $request){
        $id = $request->param("id");
        $name = $request->param('name');
        $status = $request->param('status');
        if(!$id){

            $data = [
                'name'=>$name,
                'status'=>$status,
                'create_time'=>time()
            ];
            $ProjectClass = new ProjectClassModel();
            $re_id = $ProjectClass->insertGetId($data);

            if($re_id) $resutl = ["status"=>true,"re_id"=>$re_id,"msg"=>"新增成功！"];
            else $resutl = ["status"=>false,"re_id"=>0,"msg"=>"新增失败！"];
            return json($resutl);

        }else{

            $data = [
                'name'=>$name,
                'status'=>$status,
                'update_time'=>time()
            ];
            $ProjectClass = new ProjectClassModel();
            $re_id = $ProjectClass->where("id",$id)->update($data);

            if($re_id) $resutl = ["status"=>true,"re_id"=>$re_id,"msg"=>"更新成功！"];
            else $resutl = ["status"=>false,"re_id"=>0,"msg"=>"更新失败！"];
            return json($resutl);

        }

    }

    /**
     * 删除类别
     * @param Request $request
     * @return \think\response\Json
     * @throws \Exception
     */
    public function delclass(Request $request){
        $id = $request->param('id');
        $ProjectClass = new ProjectClassModel();
        $res = $ProjectClass->where("id",$id)->delete();
        //echo $ProjectClass->getLastSql();
        if($res) $resutl = ["status"=>true,"msg"=>"删除类别成功！"];
        else $resutl = ["status"=>false,"msg"=>"删除类别失败！"];
        return json($resutl);
    }

    /**
     * 阶段列表
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function stageclass(Request $request){
        $ProjectStage = new ProjectStageModel();
        $list = $ProjectStage->select();
        if($list) $resutl = ["status"=>true,"data"=>["list"=>$list],"msg"=>"获取列表成功！"];
        else $resutl = ["status"=>false,"data"=>"","msg"=>"获取列表失败！"];
        return json($resutl);
    }

    /**
     * 添加/编辑阶段
     * @param Request $request
     * @return \think\response\Json
     */
    public function savestage(Request $request){
        $id = $request->param("id");
        $name = $request->param('name');
        $status = $request->param('status');
        if(!$id){

            $data = [
                'name'=>$name,
                'status'=>$status,
                'create_time'=>time()
            ];
            $ProjectStage = new ProjectStageModel();
            $re_id = $ProjectStage->insertGetId($data);

            if($re_id) $resutl = ["status"=>true,"re_id"=>$re_id,"msg"=>"新增成功！"];
            else $resutl = ["status"=>false,"re_id"=>0,"msg"=>"新增失败！"];
            return json($resutl);

        }else{

            $data = [
                'name'=>$name,
                'status'=>$status,
                'update_time'=>time()
            ];
            $ProjectStage = new ProjectStageModel();
            $re_id = $ProjectStage->where("id",$id)->update($data);

            if($re_id) $resutl = ["status"=>true,"re_id"=>$re_id,"msg"=>"更新成功！"];
            else $resutl = ["status"=>false,"re_id"=>0,"msg"=>"更新失败！"];
            return json($resutl);

        }
    }

    /**
     * 删除阶段
     * @param Request $request
     * @return \think\response\Json
     * @throws \Exception
     */
    public function delstage(Request $request){
        $id = $request->param('id');
        $ProjectStage = new ProjectStageModel();
        $res = $ProjectStage->where("id",$id)->delete();
        if($res) $resutl = ["status"=>true,"msg"=>"删除类别成功！"];
        else $resutl = ["status"=>false,"msg"=>"删除类别失败！"];
        return json($resutl);
    }

    public function classstagedisplaylist(Request $request){

        $ProjectClass = new ProjectClassModel();
        //$prefix = env('database.prefix', '');
        $class_id = $request->param('class_id');
        $where = "";
        if($class_id){
            $where['c.id'] = array("EQ",$class_id);
        }
        $list = $ProjectClass->alias('c')
            ->where($where)
            ->field('c.id,c.name,r.r_status as status,s.id as s_id,s.name as s_name,r.r_ischeck ')
            ->join('project_class_stage_route r','c.id=r.class_id')
            ->join('project_stage s','r.stage_id = s.id')
            ->where('r.r_status','=','1')
            ->select();

        //return $ProjectClass->getLastSql();

        $list_tmp = $ProjectClass->alias('c')
            ->DISTINCT(true)
            ->where($where)
            ->field('c.id,c.name,r.r_status as status')
            ->join('project_class_stage_route r','c.id=r.class_id')
            ->join('project_stage s','r.stage_id = s.id')
            ->where('r.r_status','=','1')
            ->select();


        $t = [];
        foreach($list_tmp as $k=>$vo){

            $t[$k]['id'] =$vo['id'];
            $t[$k]['name'] =$vo['name'];
            $t[$k]['status'] =$vo['status'];
            foreach ($list as $j=>$v){
                if(($v['id'] == $vo['id']) && $v['s_id']!==null){
                    $check_name = "";
                    $flage = 0;
                    if($v['r_ischeck']){
                        $check_name = "";
                        $flage = 1;
                    }
                    $tmp['id'] = $v['s_id'];
                    $tmp['name'] = $v['s_name'].$check_name;
                    $tmp['flage'] = $flage;
                    $t[$k]['disposeList'][]= $tmp;
                }
            }

        }
        if($list) $resutl = ["status"=>true,"data"=>["list"=>$t],"msg"=>"获取列表成功！"];
        else $resutl = ["status"=>false,"data"=>"","msg"=>"获取列表失败！"];
        return json($resutl);

    }
    /**
     * 获取配置信息列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function classstagelist(Request $request){
        $class_id = $request->param('class_id');
        $ProjectStage = new ProjectStageModel();
        $where = [];
        if($class_id) $where['class_id']= $class_id;
        $list = $ProjectStage->alias('s')
            ->field('s.id,s.name,p.class_id,p.stage_id,p.r_id as project_stageroute_id,p.r_ischeck')
            ->leftJoin('project_class_stage_route p','s.id=p.stage_id')
            ->where($where)->where('p.r_status','=','1')->select();

        if($list) $resutl = ["status"=>true,"data"=>["list"=>$list],"msg"=>"获取列表成功！"];
        else $resutl = ["status"=>false,"data"=>"","msg"=>"获取列表失败！"];
        return json($resutl);
    }

    /**
     * 新增/编辑配置
     * @param Request $request
     * @return \think\response\Json
     * @throws \Exception
     */
    public function saveclassstage(Request $request){

        $class_id = $request->param('class_id');
        $stage_id = $request->param('stage_id');
        $r_status = $request->param('r_status');
        $ProjectClassStageRoute = new ProjectClassStageRouteModel();
        $d_data['r_status'] = -1;
        $ProjectClassStageRoute->where('class_id',$class_id)->save($d_data); //删除等于class_id所有的配置
        $i = 0;
        $data = [];
        foreach($stage_id as $v){
            $data[$i]['class_id'] = $class_id;
            $data[$i]['stage_id'] = $v['id'];
            $data[$i]['r_ischeck'] = $v['flage'];
            $data[$i]['r_status'] = $r_status;
            $data[$i]["r_create_time"] = time();
            $i++;
        }
        $res = $ProjectClassStageRoute->insertAll($data);
        if($res) $resutl = ["status"=>true,"msg"=>"操作成功！"];
        else $resutl = ["status"=>false,"msg"=>"操作失败！"];
        return json($resutl);

    }

    /**
     * 设置配置状态
     * @param Request $request
     * @return \think\response\Json
     */
    public function setclassstage(Request $request){
        try {
            $class_id = $request->param('class_id');
            $stage_id = $request->param('stage_id');
            $status   = $request->param('status');
            $model = new ProjectClassStageRoute();
            $data['r_ischeck'] = $status;
            $res = $model->where('class_id','=',$class_id)->where('stage_id','=',$stage_id)->save($data);
            if($res!==false){
                $result = ['status'=>true,'msg'=>'设置配置状态成功！'];
            }else{
                $result = ['status'=>false,'msg'=>'设置配置状态失败！'];
            }
            return json($result);
        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }
    /**
     * 删除配置
     * @param Request $request
     * @return \think\response\Json
     * @throws \Exception
     */
    public function delclassstage(Request $request){
        try {
            $id = $request->param("id");
            $ProjectClassStageRoute = new ProjectClassStageRouteModel();
            $res = $ProjectClassStageRoute->where("class_id",$id)->delete();
            if($res) $resutl = ["status"=>true,"msg"=>"删除配置成功！"];
            else $resutl = ["status"=>false,"msg"=>"删除配置失败！"];
            return json($resutl);
        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }
    /**
     *
     * @param Request $request
     * @return \think\response\Json
     */
    public function getStageTimeByStage(Request $request){
        try {
            $stage_id = $request->param('stage_id');
            $project_id = $request->param('project_id');
            $ProjectInfoStageRoute = new ProjectInfoStageRoute();
            $info =  $ProjectInfoStageRoute->where('stage_id','=',$stage_id)
                                           ->where('project_id','=',$project_id)
                                           ->find();
            if(isset($info)){
                $info = $info->toArray();
                $info['start_time'] = date('Y-m-d',$info['start_time']);
                $info['end_time'] = date('Y-m-d',$info['end_time']);
                $resutl = ["status"=>true,"msg"=>"获取数据成功！","data"=>$info];
                return json($resutl);
            }else{
                $resutl = ["status"=>false,"msg"=>"获取数据失败！"];
                return json($resutl);
            }


        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }
    /**
     * 设置是否考核
     * @param Request $request
     * @return \think\response\Json
     */
    public function setProjectCheck(Request $request){
        try {
            $project_id = $request->param('id');
            $is_check = $request->param('is_check');
            $projectInfo = new ProjectInfoModel();
            $data['is_check'] = $is_check;
            $res = $projectInfo->where('id','=',$project_id)->save($data);
            if($res!==false){
                $result = ['status'=>true,'msg'=>'设置状态成功！'];
            }else{
                $result = ['status'=>false,'msg'=>'设置状态失败！'];
            }
            return json($result);
        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

}
